package br.puc.casas.gui.processing;

import br.puc.casas.model.Casa;
import processing.core.PApplet;

public class CasaAmarela extends PApplet {
	private static final long serialVersionUID = 8050978189728851253L;

	private Casa casa;
	private float xmag, ymag = 0;

	private float newXmag, newYmag = 0;

	public CasaAmarela() {
	}

	public CasaAmarela(Casa casa) {
		this.casa = casa;
	}

	public Casa getCasa() {
		return casa;
	}

	public void setCasa(Casa casa) {
		this.casa = casa;
	}

	public void setup() {
		size(500, 500, P3D);
		stroke(50);
		colorMode(RGB, 1);
	}

	public void draw() {
		background(0);
		translate(width / 2, height / 2, -30);

		newXmag = mouseX / PApplet.parseFloat(width) * TWO_PI;
		newYmag = mouseY / PApplet.parseFloat(height) * TWO_PI;

		float diff = xmag - newXmag;
		if (abs(diff) > 0.01f) {
			xmag -= diff / 4.0f;
		}

		diff = ymag - newYmag;
		if (abs(diff) > 0.01f) {
			ymag -= diff / 4.0f;
		}

		//float empena = 1.25f;
		float empena = 1 + calcularEmpena(casa);

		rotateX(-ymag);
		rotateY(-xmag);

		scale(80);

		fill(204, 102, 0, 0.5f);

		pushMatrix();

		scale(casa.getLargura(), 1, 1);

		beginShape();

		vertex(0, 0, 0);
		vertex(0, 1, 0);
		vertex(0.5f, empena, 0);
		vertex(1, 1, 0);
		vertex(1, 0, 0);

		endShape();

		beginShape();
		vertex(0, 0, 2);
		vertex(0, 1, 2);
		vertex(0.5f, empena, 2);
		vertex(1, 1, 2);
		vertex(1, 0, 2);

		endShape();

		beginShape(QUADS);

		vertex(1, 0, 0);
		vertex(1, 0, 2);
		vertex(1, 1, 2);
		vertex(1, 1, 0);

		endShape();

		beginShape(QUADS);

		vertex(0, 0, 0);
		vertex(0, 0, 2);
		vertex(0, 1, 2);
		vertex(0, 1, 0);

		endShape();

		beginShape(QUADS);

		vertex(1, 1, 0);
		vertex(0.5f, empena, 0);
		vertex(0.5f, empena, 2);
		vertex(1, 1, 2);

		endShape();

		beginShape(QUADS);

		vertex(0, 1, 0);
		vertex(0.5f, empena, 0);
		vertex(0.5f, empena, 2);
		vertex(0, 1, 2);

		endShape();

		beginShape(QUADS);

		vertex(0, 0, 0);
		vertex(1, 0, 0);
		vertex(1, 0, 2);
		vertex(0, 0, 2);

		endShape();

		popMatrix();
	}

	private float calcularEmpena(Casa casa) {
		float alpha = radians(casa.inclinacaoTelhado());

		return (casa.getLargura() / 2) * (sin(alpha) / cos(alpha));
	}
}
